#include "healthassessmentclient.h"


HealthAssessmentClient::HealthAssessmentClient(QWidget *parent)
    : QWidget(parent) {
    // 初始化 UI 组件
    heightInput = new QLineEdit(this);
    heightInput->setPlaceholderText("请输入身高 (cm)");

    weightInput = new QLineEdit(this);
    weightInput->setPlaceholderText("请输入体重 (kg)");

    heartRateInput = new QLineEdit(this);
    heartRateInput->setPlaceholderText("请输入心率 (bpm)");

    bloodPressureInput = new QLineEdit(this);
    bloodPressureInput->setPlaceholderText("请输入血压 (mmHg)");

    lungCapacityInput = new QLineEdit(this);
    lungCapacityInput->setPlaceholderText("请输入肺活量 (L)");

    QPushButton *submitButton = new QPushButton("确定", this);
    QPushButton *exitButton = new QPushButton("退出", this);

    // 布局设置
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(heightInput);
    layout->addWidget(weightInput);
    layout->addWidget(heartRateInput);
    layout->addWidget(bloodPressureInput);
    layout->addWidget(lungCapacityInput);
    layout->addWidget(submitButton);
    layout->addWidget(exitButton);
    setLayout(layout);

    // 连接信号和槽
    connect(submitButton, &QPushButton::clicked, this, &HealthAssessmentClient::evaluateHealth);
    connect(exitButton, &QPushButton::clicked, this, &HealthAssessmentClient::close);
}

void HealthAssessmentClient::evaluateHealth() {
    bool ok;
    double height = heightInput->text().toDouble(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "请正确输入身高！");
        return;
    }

    double weight = weightInput->text().toDouble(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "请正确输入体重！");
        return;
    }

    double heartRate = heartRateInput->text().toDouble(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "请正确输入心率！");
        return;
    }

    QStringList bloodPressure = bloodPressureInput->text().split('/');
    if (bloodPressure.size() != 2) {
        QMessageBox::warning(this, "输入错误", "请正确输入血压（如：120/80）！");
        return;
    }
    double systolic = bloodPressure[0].toDouble(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "请正确输入收缩压！");
        return;
    }
    double diastolic = bloodPressure[1].toDouble(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "请正确输入舒张压！");
        return;
    }

    double lungCapacity = lungCapacityInput->text().toDouble(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "请正确输入肺活量！");
        return;
    }

    // 健康评估逻辑
    QString assessmentResult = assessHealth(height, weight, heartRate, systolic, diastolic, lungCapacity);
    QMessageBox::information(this, "健康评估结果", assessmentResult);
}

QString HealthAssessmentClient::assessHealth(double height, double weight, double heartRate, double systolic, double diastolic, double lungCapacity) {
    // 评估 BMI（体重指数）
    double bmi = weight / ((height / 100) * (height / 100));
    QString healthStatus;

    if (bmi < 18.5) {
        healthStatus += "体重过轻;\n";
    } else if (bmi >= 18.5 && bmi < 24.9) {
        healthStatus += "体重正常;\n";
    } else if (bmi >= 25 && bmi < 29.9) {
        healthStatus += "超重;\n";
    } else {
        healthStatus += "肥胖;\n";
    }

    // 心率评估
    if (heartRate < 60) {
        healthStatus += "心率偏低;\n";
    } else if (heartRate > 100) {
        healthStatus += "心率偏高;\n";
    } else {
        healthStatus += "心率正常;\n";
    }

    // 血压评估
    if (systolic < 90 || diastolic < 60) {
        healthStatus += "血压偏低;\n";
    } else if (systolic > 140 || diastolic > 90) {
        healthStatus += "血压偏高;\n";
    } else {
        healthStatus += "血压正常;\n";
    }

    // 肺活量评估（简单判断）
    if (lungCapacity < 3) {
        healthStatus += "肺活量偏低;\n";
    } else if (lungCapacity > 6) {
        healthStatus += "肺活量偏高;\n";
    } else {
        healthStatus += "肺活量正常;\n";
    }

    return healthStatus;
}
